import { ref, unref } from 'vue'
import { adapter, request } from '@/request'
import { reportKeywordByGet } from '@/core/api/dashboard'
import type { Order } from '@/core/interfaces/order'
import type { OrderKeyword } from '@/core/interfaces/keyword'
import type { RequestWrapper } from '@/core/interfaces/common'
import { PlatformKeyMap } from '@/core/mapper/platform'

interface Params {
  orderId: Order['id']
}

export default function getOrderKeyword() {
  const service = adapter<RequestWrapper<OrderKeyword[]>>(request)
  const response = ref<OrderKeyword[]>([])
  const loading = ref(false)

  const fetch = (params: Params) => {
    const { url, method } = reportKeywordByGet()
    loading.value = true

    return service({
      config: {
        url,
        method,
        params: unref(params)
      },
      callback: (res) => {
        response.value = res.data.data.map((item) => {
          return {
            ...item,
            label: `${item.name}（${PlatformKeyMap[item.clientType]}）`,
            value: item.id
          }
        })
      },
      error: (err) => {
        console.log(err)
      },
      done: () => {
        loading.value = false
      }
    })
  }

  return [response, fetch, loading] as const
}
